package meijuli.com.join51.business.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import meijuli.com.join51.business.R;
import com.jhx.common.model.User;
import meijuli.com.join51.business.utils.QREncoder;
import com.jhx.common.utils.UserManager;

/**
 * 作者:Ryan
 * 描述:我的二维码
 */
public class MyQRFragment extends BaseFragment {
    ImageView mHeadImageView;
    TextView mNicknameView;
    ImageView mSexTagView;
    LinearLayout mNicknameContainer;
    TextView mRegionNameView;
    ImageView mQRCodeView;
    TextView mMyPromotionCodeView;
    /**
     * 二维码地址
     */
    private String codeUrl;

    public static MyQRFragment newInstance() {

        Bundle args = new Bundle();
        MyQRFragment fragment = new MyQRFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_qr, container, false);
        mHeadImageView = view.findViewById(R.id.head_image_view);
        mNicknameView = view.findViewById(R.id.nicknameView);
        mSexTagView = view.findViewById(R.id.sexTagView);
        mNicknameContainer = view.findViewById(R.id.nicknameContainer);
        mRegionNameView = view.findViewById(R.id.regionNameView);
        mQRCodeView = view.findViewById(R.id.QRCodeView);
        mMyPromotionCodeView = view.findViewById(R.id.myPromotionCodeView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        User user = UserManager.get().getUser();
        if (UserManager.get().checkUserNull(mActivity)) {
            return;
        }
        codeUrl = user.getQrcode();
        QREncoder.generateQRCodeImage(codeUrl, mQRCodeView, 180, 0xFF30ABEF);

        Glide.with(this).load(user.getHeadImage()).error(R.mipmap.icon_header)
                .into(mHeadImageView);
        mNicknameView.setText(user.getBusinessName());

        mMyPromotionCodeView.setText(String.format("推广码：%s",
                TextUtils.isEmpty(user.getPhone()) ?
                        "暂无" : user.getPhone()));
    }

    public String getCodeUrl() {
        return codeUrl;
    }

}
